package com.ruoyi.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.domain.Ability_ServiceInfor;
import com.ruoyi.domain.Ability_SubscribeInfor;
import com.ruoyi.domain.Ability_UserInfor;
import com.ruoyi.domain.Ability_UserLimitingInfor;
import com.ruoyi.mapper.Ability_ServiceInforMapper;
import com.ruoyi.mapper.Ability_SubscribeInforMapper;
import com.ruoyi.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;

import java.util.List;
@Service("Ability_SubscribeInforService")
public class Ability_SubscribeInforServiceImpl implements Ability_SubscribeInforService {
    @Autowired
    Ability_SubscribeInforMapper ability_SubscribeInforMapper;
    @Autowired
    SubscribeKong subscribeKong;
    @Autowired
    Ability_UserLimitingService abilityUserLimitingService;
    @Autowired
    Ability_ServiceLimitingService abilityServiceLimitingService;
    @Autowired
    Ability_AbiltyService ability_AbiltyService;
    @Autowired
    Ability_UserService ability_UserService;

    @Override
    public List<Ability_SubscribeInfor> getAbility_SubscribeInforList(Ability_SubscribeInfor ability_SubscribeInfor) {

        List<Ability_SubscribeInfor>  list= ability_SubscribeInforMapper.getAbility_SubscribeInforList(ability_SubscribeInfor);

        return list;
    }

    @Override
    public int insertAbility_SubscribeInfor(Ability_SubscribeInfor ability_SubscribeInfor) {
        String serviceName=ability_SubscribeInfor.getServiceName();
        String userAccount=ability_SubscribeInfor.getUserAccount();
        ability_SubscribeInfor.setStatus(0);

        Ability_ServiceInfor ability_serviceInfor=ability_SubscribeInforMapper.checkService(serviceName);
       // System.out.println(ability_SubscribeInforMapper.checkService(serviceName)==null);
        Ability_UserInfor ability_userInfor=null;
        if(ability_serviceInfor!=null&&ability_serviceInfor.getStatus()==1){
             ability_userInfor=ability_SubscribeInforMapper.checkUser(userAccount);
            if(ability_userInfor!=null){
                ability_SubscribeInfor.setServiceId(ability_serviceInfor.getUUID());
                ability_SubscribeInfor.setUserId(ability_userInfor.getId());
            }else{
                return -102;
            }
        }else{
            return -101;
        }
        if(    ability_SubscribeInforMapper.checkSubscribe(ability_SubscribeInfor.getServiceId(),ability_SubscribeInfor.getUserId())!=null){
            return -104;
        }
        int k=0;
        k=  ability_SubscribeInforMapper.insertAbility_SubscribeInfor(ability_SubscribeInfor);
     //   String res="";
//          try {
//
//              res=   subscribeKong.addSubscribe(ability_serviceInfor.getServiceNameC(),ability_userInfor.getCname(),ability_serviceInfor.getAllowgroup());
//              JSONObject jsonObject=JSONObject.parseObject(res);
//              if(jsonObject.getString("status").equals("200")||jsonObject.getString("status").equals("201")){
//                  k=  ability_SubscribeInforMapper.insertAbility_SubscribeInfor(ability_SubscribeInfor);
//                  if(k<=0){
//                      subscribeKong.delSubscribe(ability_serviceInfor.getServiceNameC(),ability_userInfor.getAccount(),ability_serviceInfor.getAllowgroup());
//                  }
//              }else{
//                  subscribeKong.delSubscribe(ability_serviceInfor.getServiceNameC(),ability_userInfor.getAccount(),ability_serviceInfor.getAllowgroup());
//                  return -103;
//
//              }
//          } catch (Exception e){
//           k=-103;
//          }


        return k;
    }

    @Override
    public int updateAbility_SubscribeInfor(Ability_SubscribeInfor ability_SubscribeInfor) {

        return ability_SubscribeInforMapper.updateAbility_SubscribeInfor(ability_SubscribeInfor);
    }

    @Override
    public int deleteAbility_SubscribeInforById(Integer id) {

        int k=0;
        Ability_SubscribeInfor abilitySubscribeInfor=ability_SubscribeInforMapper.selectAbility_SubscribeInforById(id);
        Ability_ServiceInfor ability_serviceInfor=ability_AbiltyService.selectAbility_ServiceInforById(abilitySubscribeInfor.getServiceId());
      Ability_UserLimitingInfor ability_userLimitingInfor=new Ability_UserLimitingInfor();
       if(abilitySubscribeInfor==null){
           return -1;
       }else {
            ability_userLimitingInfor.setSid(abilitySubscribeInfor.getServiceId());
            ability_userLimitingInfor.setUid(abilitySubscribeInfor.getUserId());
            System.out.println("000000"+ability_userLimitingInfor.getUid());
       }
        String getresp="";
        String msg="";
        try {
            getresp=subscribeKong.getInfor("consumers/"+abilitySubscribeInfor.getUserAccount()+"/acls/"+ability_serviceInfor.getAllowgroup());

        }catch (HttpClientErrorException ex){
          //  System.out.println("HTTP Error: " + ex.getStatusCode() + " - " + ex.getResponseBodyAsString());
            msg=ex.getStatusCode().toString();
        }
       // System.err.println(msg);
        JSONObject jsongetresp=JSONObject.parseObject(getresp);
        if(!msg.equals("404 NOT_FOUND")){
            try {
                String res=subscribeKong.delSubscribe(abilitySubscribeInfor.getServiceName(),abilitySubscribeInfor.getUserAccount(),ability_serviceInfor.getAllowgroup());
                JSONObject jsonObject=JSONObject.parseObject(res);
                if(jsonObject.getString("status").equals("")||jsonObject.getString("status").equals("204")){
                    k=  ability_SubscribeInforMapper.deleteAbility_SubscribeInforById(id);
                    abilityUserLimitingService.deleteAbility_UserLimitingInforBySUID(ability_userLimitingInfor);
                }

            } catch (Exception e){
                k=-1;
            }
        }else {
            k=  ability_SubscribeInforMapper.deleteAbility_SubscribeInforById(id);
        }

        return k;
    }

    @Override
    public int deleteAbility_SubscribeInforByIds(Integer[] ids) {
        return 0;
    }

    @Override
    public Ability_SubscribeInfor selectAbility_SubscribeInforById(Integer id) {
        return ability_SubscribeInforMapper.selectAbility_SubscribeInforById(id);
    }

    @Override
    public int Approved(Ability_SubscribeInfor abilitySubscribeInfor) {
        int k=0;
       if(abilitySubscribeInfor.getStatus()==0){
           Ability_ServiceInfor ability_serviceInfor=ability_AbiltyService.selectAbility_ServiceInforById(abilitySubscribeInfor.getServiceId());
          if(ability_serviceInfor==null){
              return -2;
          }
           Ability_UserInfor ability_userInfor=ability_UserService.selectUserInforById(abilitySubscribeInfor.getUserId());
          if(ability_userInfor==null){
              return -3;
          }
        Ability_SubscribeInfor ability_SubscribeInfor1=  ability_SubscribeInforMapper.checkSubscribe(ability_serviceInfor.getUUID(),ability_userInfor.getId());
          if (ability_SubscribeInfor1==null){
              return -4;
          }
          String res="";
          try {
               res=subscribeKong.addSubscribe(ability_serviceInfor.getServiceNameE(),ability_userInfor.getCname(),ability_serviceInfor.getAllowgroup());
          }catch (Exception e)
          {
              e.printStackTrace();
              return -1;
          }
           JSONObject jsonObject=JSONObject.parseObject(res);
           if(jsonObject.getString("status").equals("200")||jsonObject.getString("status").equals("201")){
               abilitySubscribeInfor.setStatus(1);
               abilitySubscribeInfor.setId(ability_SubscribeInfor1.getId());
              k=  ability_SubscribeInforMapper.updateAbility_SubscribeInfor(abilitySubscribeInfor);
           }
           //添加订阅节流
           Ability_UserLimitingInfor abilityUserLimitingInfor=new Ability_UserLimitingInfor();
           abilityUserLimitingInfor.setSid(ability_serviceInfor.getUUID());
           abilityUserLimitingInfor.setUid(ability_userInfor.getId());
           abilityUserLimitingInfor.setSname(ability_serviceInfor.getServiceNameE());
           abilityUserLimitingInfor.setAcount(ability_userInfor.getAccount());
           String gruop[]=abilityServiceLimitingService.selectAbility_ServiceLimitingInforBySid(ability_serviceInfor.getUUID()).get(0).getLevelgroup();
           abilityUserLimitingInfor.setPid(jsonObject.getJSONObject("consumer").getString("id"));
           if(gruop!=null&gruop.length>0){
           abilityUserLimitingInfor.setLevel(Integer.parseInt(gruop[0]));
           abilityUserLimitingService.insertAbility_UserLimitingInfor(abilityUserLimitingInfor);
       }else{
           System.err.println("服务节流为空，服务ID："+ability_serviceInfor.getUUID());
       }
       }else{
           k=-5;
       }

        return k;
    }
}
